package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description RoleController
 * @Author loe
 * @Date 2024-07-15
 */

@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色分页
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> limitSearch(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ) {
        PageResponse<RoleVo> pageResponse = roleService.limitSearch(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 角色添加
     * @param roleDto
     * @return
     */
    @PutMapping
    @ApiOperation("角色添加")
    @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName", "roleDto.dataState"})
    public ResponseResult insertRole(@RequestBody RoleDto roleDto) {
        roleService.insertRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 根据角色查询选中的资源数据
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult<List<String>> getResourceNoByRoleId(
            @PathVariable Long roleId
    ) {
        List<String> stringList = roleService.getResourceNoByRoleId(roleId);
        return ResponseResult.success(stringList);
    }

    /**
     * 角色修改
     * @return
     */
    @PatchMapping
    @ApiOperation("角色修改")
    @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {
            "roleDto.dataScope",
            "roleDto.id",
            "roleDto.checkedDeptNos",
            "roleDto.roleName",
            "roleDto.dataState",
            "roleDto.checkedResourceNos"
    })
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public ResponseResult deleteRole(@PathVariable String roleId) {
        roleService.deleteRole(roleId);
        return ResponseResult.success();
    }

    /**
     * 角色下拉框
     * @return
     */
    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult<List<RoleVo>> getRoleList() {
        List<RoleVo> roleVoList = roleService.getRoleList();
        return ResponseResult.success(roleVoList);
    }

}
